from django.db import models


# Create your models here.

# 修改用户表, 关联免费版价格策略, 默认为免费版, 并且在交易记录表中添加信息
# 用户表
class UserInfo(models.Model):
    username = models.CharField(verbose_name='用户名', max_length=32, unique=True)
    password = models.CharField(verbose_name='密码', max_length=64)
    email = models.EmailField(verbose_name='邮箱', unique=True)
    mobile_phone = models.CharField(verbose_name='手机号', max_length=11, unique=True)

    # 关联免费版价格策略, 默认为免费版
    price_policy = models.ForeignKey(verbose_name='关联的价格策略', to='PricePolicy', on_delete=models.CASCADE,
                                     null=True, default=1)
    # 在交易记录表中添加信息
    transaction = models.ForeignKey(verbose_name='关联的交易记录', to='Transaction', on_delete=models.CASCADE,
                                    null=True)

    # 重写__str__方法, 用于在后台显示用户信息
    def __str__(self):
        return self.username


# 价格策略表
class PricePolicy(models.Model):
    category_choices = (
        (1, '免费版'),
        (2, '收费版'),
        (3, '其他')
    )
    category = models.SmallIntegerField(verbose_name='收费类型', choices=category_choices)
    title = models.CharField(verbose_name='标题', max_length=32)
    # 价格, 正整数, 单位: 元
    price = models.PositiveIntegerField(verbose_name='价格(单位:元)')
    project_num = models.PositiveIntegerField(verbose_name='项目数')
    project_member = models.PositiveIntegerField(verbose_name='项目成员数')
    project_space = models.PositiveIntegerField(verbose_name='单项目空间(M)')
    per_file_size = models.PositiveIntegerField(verbose_name='单文件大小(M)')
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    # 重写__str__方法, 用于在后台显示价格策略信息
    def __str__(self):
        return self.title


# 创建交易记录表
class Transaction(models.Model):
    status_choices = (
        (1, '未支付'),
        (2, '已支付'),
    )
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choices)
    # 订单号, 唯一索引
    order = models.CharField(verbose_name='订单号', max_length=64, unique=True)

    user = models.ForeignKey(verbose_name='关联的用户', to='UserInfo', on_delete=models.CASCADE,
                             related_name='user_transaction')
    price_policy = models.ForeignKey(verbose_name='关联的价格策略', to='PricePolicy', on_delete=models.CASCADE,
                                     related_name='transaction')
    count = models.IntegerField(verbose_name='数量(年)', help_text='0表示无限期')
    price = models.IntegerField(verbose_name='实际支付价格')

    start_datetime = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    end_datetime = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


# 项目表
class Project(models.Model):
    color_choices = (
        (1, '#e6a23c'),
        (2, '#f56c6c'),
        (3, '#5cb87a'),
        (4, '#1989fa'),
        (5, '#6f7ad3'),
        (6, '#909399'),
    )
    # 项目名称
    name = models.CharField(verbose_name='项目名称', max_length=32)
    # 项目颜色
    color = models.SmallIntegerField(verbose_name='颜色', choices=color_choices, default=1)
    # 项目概述
    desc = models.CharField(verbose_name='项目概述', max_length=255, default=None, null=True, blank=True)
    # 项目已使用空间
    use_space = models.BigIntegerField(verbose_name='已使用空间', default=0)
    # 星标
    star = models.BooleanField(verbose_name='星标', default=False)
    # 参与人数
    join_count = models.IntegerField(verbose_name='参与人数', default=1)
    # 创建者
    creator = models.ForeignKey(verbose_name='创建者', to='UserInfo', on_delete=models.CASCADE)
    # 创建时间
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    # project_user = models.ManyToManyField(verbose_name='参与者', to='UserInfo', through='ProjectUser',
    #                                       through_fields=('project', 'user'))
    bucket = models.CharField(verbose_name='cos桶', max_length=128, null=True, blank=True)
    region = models.CharField(verbose_name='cos区域', max_length=128, null=True, blank=True)


# 创建参与者表
class ProjectUser(models.Model):
    # 有两个外键时, 需要指定related_name, 否则会报错
    project = models.ForeignKey(verbose_name='参与的项目', to='Project', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='参与的用户', to='UserInfo', on_delete=models.CASCADE)
    star = models.BooleanField(verbose_name='星标', default=False)
    # 邀请者
    # invitee = models.ForeignKey(verbose_name='邀请者', to='UserInfo', on_delete=models.CASCADE, related_name='invites',
    #                             null=True, blank=True)
    create_datetime = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)


# wiki表
class Wiki(models.Model):
    title = models.CharField(verbose_name='标题', max_length=64)
    content = models.TextField(verbose_name='内容')
    project = models.ForeignKey(verbose_name='项目', to='Project', default=None)

    depth = models.IntegerField(verbose_name='深度', default=1)

    # 自关联
    parent = models.ForeignKey(verbose_name='父文章', to='Wiki', null=True, blank=True, related_name='children')

    def __str__(self):
        return self.title


# 文件表
class FileRepository(models.Model):
    project = models.ForeignKey(verbose_name='项目', to='Project')

    fi_type_cho = (
        (1, '文件'),
        (2, '文件夹')
    )
    file_type = models.SmallIntegerField(verbose_name='类型', choices=fi_type_cho)
    name = models.CharField(verbose_name='文件夹名称', max_length=32, help_text='文件/文件夹')
    key = models.CharField(verbose_name='文件储存在COS中的key', max_length=128, blank=True, null=True)

    file_size = models.IntegerField(verbose_name='文件大小', blank=True, null=True)
    file_path = models.CharField(verbose_name='文件路径', max_length=255, null=True, blank=True)

    parent = models.ForeignKey(verbose_name='父级目录', to="self", related_name='child', null=True, blank=True)
    update_user = models.ForeignKey(verbose_name='最近更新者', to='UserInfo')
    update_datetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)
